package com.geekcoding.geekoj.controller.admin;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.geekcoding.common.dto.ApiResponse;
import com.geekcoding.common.dto.BasePageQuery;
import com.geekcoding.common.exceptions.GeekRuntimeException;
import com.geekcoding.geekoj.dto.request.QuestionTagAddReq;
import com.geekcoding.geekoj.entity.*;
import com.geekcoding.geekoj.mapper.SubjectQuestionTagMapper;
import com.geekcoding.geekoj.repository.QuestionRepository;
import com.geekcoding.geekoj.repository.SubjectQuestionTagRepository;
import com.geekcoding.geekoj.repository.SubjectTagRepository;
import com.geekcoding.geekoj.repository.TagRepository;
import com.geekcoding.geekoj.service.UserService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;
import java.util.Optional;

@RestController()
@RequestMapping("/api/admin/tag")
public class AdminTagController extends BaseAdminController<SubjectQuestionTag, SubjectQuestionTag, SubjectQuestionTagMapper, SubjectQuestionTagRepository> {
    private final TagRepository tagRepository;

    private final SubjectTagRepository subjectTagRepository;

    private final QuestionRepository questionRepository;

    public AdminTagController(SubjectQuestionTagRepository repository, UserService userService, TagRepository tagRepository, SubjectTagRepository subjectTagRepository, QuestionRepository questionRepository) {
        super(repository, userService);
        this.tagRepository = tagRepository;
        this.subjectTagRepository = subjectTagRepository;
        this.questionRepository = questionRepository;
    }


    @PostMapping("/add")
    public ApiResponse addTag(@RequestBody QuestionTagAddReq req) {
        Question question = Optional.ofNullable(questionRepository.getById(req.getQuestionId())).orElseThrow(() -> new GeekRuntimeException("问题不存在"));
        Tag tag = tagRepository.getOne(Wrappers.<Tag>lambdaQuery().eq(Tag::getTagName, req.getTagName()));
        if (Objects.isNull(tag)) {
            tag = Tag.builder().tagName(req.getTagName()).build();
            tagRepository.save(tag);
        }
        SubjectTag subjectTag = subjectTagRepository.getOne(Wrappers.<SubjectTag>lambdaQuery().eq(SubjectTag::getSubjectId, question.getSubjectId()).eq(SubjectTag::getTagId, tag.getId()));
        if (Objects.isNull(subjectTag)) {
            subjectTag = SubjectTag.builder().tagId(tag.getId()).subjectId(question.getSubjectId()).build();
            subjectTagRepository.saveOrUpdate(subjectTag);
        }
        SubjectQuestionTag subjectQuestionTag = repository.getOne(Wrappers.<SubjectQuestionTag>lambdaQuery().eq(SubjectQuestionTag::getQuestionId, question.getId()).eq(SubjectQuestionTag::getSubjectTagId, subjectTag.getId()));
        if (Objects.isNull(subjectQuestionTag)) {
            subjectQuestionTag = SubjectQuestionTag.builder().questionId(question.getId()).subjectTagId(subjectTag.getId()).build();
            repository.saveOrUpdate(subjectQuestionTag);
        }
        return ApiResponse.success(question);
    }

    @PostMapping("/list")
    public ApiResponse tagPageList(@RequestBody BasePageQuery query) {
        Page<Tag> tPage = tagRepository.queryByPage(Page.of(query.getCurrent(), query.getPageSize()), null);
        return ApiResponse.success(tPage);
    }
    @PostMapping("/edit")
    public ApiResponse editTag(@RequestBody Tag req) {
        tagRepository.saveOrUpdate(req);
        return ApiResponse.success(req);
    }

}
